<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" style="margin:10px 10px;">
  <el-form-item label="商品分类名称" prop="catname">
    <el-input v-model="ruleForm.catname"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="图标" prop="catname">
	<table>
		<tr>
			<td><img :src="ruleForm.icon"  class="doctor-upload-image"></td>
			<td>
				<el-upload
				  class="upload-demo"
				  :on-success="iconSuccess"
				  :show-file-list="false"
				  action="http://www.yiru520.com/admin/index.php/Goods/upload">
				  <el-button size="small" type="primary">点击上传</el-button>
				</el-upload>
			</td>
		</tr>
	</table>
  
  
    
  </el-form-item>
  <el-form-item label="排序" prop="listorder">
    <el-input v-model="ruleForm.listorder"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          catname: '',
          listorder: '',
          icon: '',
        },
        rules: {
          catname: [
            { required: true, message: '请输入商品分类名称', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          listorder: [
            { required: true, message: '排序不能为空'}
          ],
         
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
				method:"post",
				url:this.host+"/admin/index.php/Goods/saveCategory",
				data:{
					catid : this.$route.query.catid,
					catname: this.ruleForm.catname,
					listorder: this.ruleForm.listorder,
					icon: this.ruleForm.icon,
				}
			}).then((res)=>{
				this.$message({
				  message: '保存成功',
				  type: 'success'
				});
				this.$router.push({ path: 'category' })
			})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
	  iconSuccess(evnet, file) {
	    if(file.response == 1){
			this.$message({
				  message: '图片已存在',
				  type: 'success'
				});
		}else{
			this.ruleForm.icon = file.response
		}
	  },
	  getCategory(){
		this.$axios({
			method:"post",
			url:this.host+"/admin/index.php/Goods/getCategory",
			data:{
				catid : this.$route.query.catid
			}
		}).then((res)=>{

			this.ruleForm.catname = res.data.catname 
			this.ruleForm.listorder = res.data.listorder
			this.ruleForm.icon = res.data.icon
		})
	  }
    },
	mounted () {
		if(this.$route.query.catid)
			this.getCategory();	
	}
  }
</script>